package com.acme.bankapp.service;

import java.util.Iterator;

import com.acme.bankapp.service.Email;
import com.acme.bankapp.domain.Client;

public class EmailService {
	private Queue queueList = new Queue();
	ClientThread t = new ClientThread();
	public EmailService() {
		t.start();
	}
	public void sendNotificationEmail(Client client, String email) {
		Email mail = new Email(client, email);
		queueList.add(mail);
	}
	public Queue getQueue() {
		return queueList;
	}
	public void close() {
			while (!(queueList.getQueue().isEmpty())) {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		t.interrupt();
	}
	public class ClientThread extends Thread {
		@Override
		public void run() {
			synchronized(queueList) {
				Iterator<Email> iter;
				while (true) {
					while (!(queueList.getQueue().isEmpty())) {
						iter = queueList.getQueue().iterator();
						while (iter.hasNext())
							queueList.delete();
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						return;
					}
				}
			}
		}
	}
}
